package com.jiuling.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jswzc.api.service.demo.DubboServiceI;
import com.jswzc.api.service.emailUnified.EmailUnifiedServiceI;
import com.jswzc.api.service.smsUnified.SmsUnifiedServiceI;
import com.jswzc.api.service.wxTemplate.WxTemplateServiceI;
import com.jswzc.framework.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liujiajia on 2017/5/9.
 */
@Controller
@RequestMapping("/demo")
public class DemoController extends BaseController {

    @Reference
    private DubboServiceI dubboService;
    @Reference
    private WxTemplateServiceI wxTemplateService;
    @Reference
    private EmailUnifiedServiceI emailUnifiedService;
    @Reference
    private SmsUnifiedServiceI smsUnifiedService;


    /**
     * 微信模版消息
     * 购买成功通知
     * 模版短号  TM00001
     * @param response
     * @throws Exception
     */
    /**
     *
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public void testDemo(HttpServletResponse response) throws Exception {
        if (dubboService != null) {
            String ss=wxTemplateService.sendTemplate("obisKwf0N2Q5nt0VJdll4iuVO7nE",
                    "9PZHAI11eUa_YE--QtEivC3L7vdUczKAZ1jRKGbnWWBX-GPvGk9BkU_iWOJV3TMsQa0SIHRgDQ-Cao6OlFSdELMGpDuO5aoJm5Y44kuUAScn5ZymH82neBQzgH6ov8HSVPSjAEABVY",
                    "obisKwckFFbkYovn5YMtGJXdInVg",
                    "TM00001",
                    "www.baidu.com",
                    "{\n"+
                            "\"name\":{\n" +
                            "\"value\":\"微信电影票\",\n" +
                            "\"color\":\"#173177\"\n" +
                            " } ,\n" +
                            "\"remark\": {\n" +
                            "\"value\":\"有效期：2017-6-19\\n券号为:50248182739\\n密码为:123456890\",\n" +
                            "\"color\":\"#173177\"\n" +
                            " }\n" +
                            "}");

            writeJson(ss, response);
        }
    }


    /**
     * 测试短信
     * @param response
     * @param mobile        需要发送的号码
     * @param content       此次发送的短信内容
     * @param smsCode       由中间件定义的类型，具体参考接口文档
     * @param smsAuthorCode 接口调用方的授权码
     * @throws Exception
     */
    @RequestMapping(value = "/sms", method = RequestMethod.GET)
    public void smsDemo(HttpServletResponse response, String mobile, String content, String smsCode,String smsAuthorCode) throws Exception {
        Map<String, Object> res = new HashMap<String, Object>();
        try {
            res=smsUnifiedService.sendSms(mobile, content, smsCode,smsAuthorCode);
        } catch (Exception e) {
            res.put("res", "F");
            res.put("msg", "未知错误:" + e.getMessage());
        }
        writeJson(res, response);
    }




    /**
     * 测试邮件
     * @param response
     * @param addTo     邮件发送目的地
     * @param msg       邮件发送内容
     * @param code      中间件自定义的code 表示具体的调用配置，具体参考接口文档
     * @throws Exception
     */
    @RequestMapping(value = "/email", method = RequestMethod.GET)
    public void emailDemo(HttpServletResponse response,String addTo,String addFrom,String sub,String msg,String smtpType,String username,String password,String port) throws Exception {
        Map<String, Object> res = new HashMap<String, Object>();
        try {
            res = emailUnifiedService.sendEmail(addTo, addFrom, sub, msg, smtpType, username, password, port);
        } catch (Exception e) {
            res.put("res", "F");
            res.put("msg", "未知错误:" + e.getMessage());
        }
        writeJson(res, response);
    }



}
